<template>
	<view class="content">
		<view class="size" @tap="createAdView">创建广告控件</view>
		<view class="size" @tap="createFullScreenVideoAd">创建全屏视频广告</view>
		<view class="size" @tap="createInterstitialAd">创建插屏广告</view>
		<view class="size" @tap="createRewardedVideoAd">创建激励视频广告</view>
		<view class="size" @tap="getAds">获取信息流广告数据</view>
		<view class="size" @tap="getPersonalizedAd">获取是否开启个性化推荐广告</view>
		<view class="size" @tap="releaseAdData">释放广告数据</view>
		<view class="size" @tap="setPersonalizedAd">设置是否开启个性化推荐广告</view>
		<view class="size" @tap="showContentPage">显示内容联盟页面</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				adView: null,
				adFull:null,
				adInter:null,
				adReward:null,
				adView:null
			}
		},
		methods: {
			createAdView() {
				this.adView = plus.ad.createAdView({
					top: '20px',
					left: '0px',
					width: '100%',
					height: '0px',
					position: 'static'
				});
			},
			createFullScreenVideoAd() {
				if (this.adFull) {
					console.log('正在加载全屏视频广告111');
					return;
				}
				console.log('#全屏视频广告#');
				this.adFull = plus.ad.createFullScreenVideoAd({
					adpid: '1507000611'
				}); //1507000611是HBuilder测试基座使用的广告位，正式环境下请使用在uni-AD后台申请的
				this.adFull.onLoad(function() {
					console.log('加载成功')
					this.adFull.show();
				});
				this.adFull.onError(function(e) {
					console.log('加载失败: ' + JSON.stringify(e));
					this.adFull.destroy();
					this.adFull = null;
				});
				this.adFull.onClose(function(e) {
					if (e.isEnded) {
						console.log('全屏视频播放完成');
						plus.nativeUI.toast('全屏视频播放完成');
						//这里实现完成全屏视频逻辑
					} else {
						console.log('全屏视频未播放完成关闭!')
					}
					this.adFull.destroy();
					this.adFull = null;
				});
				this.adFull.load();
			},
			createInterstitialAd(){
				if(this.adInter){
				    console.log('正在加载插屏广告');
				    return;
				  }
				  console.log('#插屏广告#');
				  this.adInter = plus.ad.createInterstitialAd({adpid:'1111111113'}); //1111111113是HBuilder测试基座使用的广告位，正式环境下请使用在uni-AD后台申请的广告位
				  this.adInter.onLoad(function(){
				    console.log('加载成功')
				    this.adInter.show();
				  });
				  this.adInter.onError(function(e){
				     console.log('加载失败: '+JSON.stringify(e));
				    this.adInter.destroy();
				    this.adInter = null;
				  });
				  this.adInter.onClose(function(e){
				    console.log('插屏广告被关闭');
				    this.adInter.destroy();
				    this.adInter = null;
				  });
				  this.adInter.load();
			},
			createRewardedVideoAd(){
				if(this.adReward){
				    console.log('正在加载激励视频广告');
				    return;
				  }
				   console.log('#视频激励广告#');
				  this.adReward = plus.ad.createRewardedVideoAd({adpid:'1507000689'});
				  this.adReward.onLoad(function(){
				     console.log('加载成功')
				    this.adReward.show();
				  });
				  this.adReward.onError(function(e){
				     console.log('加载失败: '+JSON.stringify(e));
				    this.adReward.destroy();
				    this.adReward = null;
				  });
				  this.adReward.onClose(function(e){
				    if(e.isEnded){
				       console.log('激励视频播放完成');
				      plus.nativeUI.toast('激励视频播放完成');
				      //这里实现完成激励视频逻辑
				    }else{
				       console.log('激励视频未播放完成关闭!')
				    }
				    this.adReward.destroy();
				    this.adReward = null;
				  });
				  this.adReward.load();
			},
			getAds(){
				plus.ad.getAds({adpid:'1111111111',width:'100%',count:1}, function(e){
				    console.log('获取广告成功: '+JSON.stringify(e));
				    if(!e || !e.ads || e.ads.length<1){
				      console.log('无广告数据！');
				    }else{
				      console.log('开始渲染广告');
				      this.adView.renderingBind(e.ads[0]);
				    }
				  }, function(e){
				    console.log('获取广告失败: '+JSON.stringify(e));
				  });
			},
			getPersonalizedAd(){
				// true 开启， false 失败
				const ad = plus.ad.getPersonalizedAd();
				console.log("ad=>",ad)
			},
			releaseAdData(){
				const adData = "";
				plus.ad.releaseAdData(data);
			},
			setPersonalizedAd(){
				const enable = true // true or false
				plus.ad.setPersonalizedAd(enable)
			},
			showContentPage(){
				//1.申请开通某个应用的uni-ad，https://uniad.dcloud.net.cn/list/app
				plus.ad.showContentPage({
				    adpid:'__UNI__23F5645'    //应用即项目的APPID
				  }, function(){
				    console.log('显示内容联盟成功!');
				  }, function(e){
						Object.keys(e).forEach(key=>{
							console.log( key + e[key]);
						})
				})
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.size {
		margin: 30rpx 0;
	}
</style>
